﻿package ua.mihaylenko.app.controller 
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.filters.BlurFilter;
	import flash.geom.Point;
	import vk.gui.Box;
	/**
	 * ...
	 * @author Mihaylenko A.L.
	 */
	public class APPStage
	{
		static public const APP_CONTENT_URL:String = 'content/';
		static private var _appStage:DisplayObjectContainer;
		static private var _pGameResolution:Point;
		static private var _aDisplayList:Array;
		static private var _bmdLock:Bitmap;
		static private var VKBox:Box;
		public function APPStage() 
		{
			
		}
		static public function set appStage($appStage:DisplayObjectContainer):void {
			
			if (!_appStage) {
				_appStage = $appStage; 
				_pGameResolution = new Point(_appStage.stage.stageWidth, _appStage.stage.stageHeight);
			}
		}
		static public function get gameResolution():Point { return _pGameResolution; }
		static public function get appStage():DisplayObjectContainer { return _appStage; }
		static public function error(sError:String):void {
			
			lockStage = true;
			VKBox = new Box("!Erorr!", sError, 0, _pGameResolution.x / 2, ['Ok']);
			VKBox.x = (_pGameResolution.x - VKBox.width) / 2;
			VKBox.y = (_pGameResolution.y - VKBox.height) / 2;
			VKBox.addEventListener(Event.SELECT, onCloseError);
			VKBox.visible = true;
			_appStage.addChild(VKBox);
		}
		
		static private function onCloseError(e:Event):void 
		{
			VKBox.removeEventListener(Event.SELECT, onCloseError);
			if (_appStage.contains(VKBox))_appStage.removeChild(VKBox);
			VKBox = null;
			//lockStage = false;
		}
		static public function set lockStage(bValue:Boolean):void {
			if (bValue) {
				if (!_appStage) return;
					else if (!_appStage.numChildren) return;
				
				if (_bmdLock) {
					if (_bmdLock.bitmapData)_bmdLock.bitmapData.dispose();
				}
				_bmdLock = new Bitmap(new BitmapData(_pGameResolution.x, _pGameResolution.y, true, 0x000000));
				_bmdLock.bitmapData.draw(_appStage);
				_bmdLock.smoothing = true;
				_bmdLock.filters = [new BlurFilter(10, 10, 3)];
				_aDisplayList = [];
				while (_appStage.numChildren) {
					_aDisplayList.push(_appStage.removeChildAt(0));
				}
				_appStage.addChild(_bmdLock);
			}else {
				if (!_aDisplayList) return;
					else if (!_aDisplayList.length) return;
						else if (!_appStage) return;
							else if (!_bmdLock) return;
				if (_appStage.contains(_bmdLock))_appStage.removeChild(_bmdLock);
				if (_bmdLock.bitmapData)_bmdLock.bitmapData.dispose();
				_bmdLock = null;
				while (_aDisplayList.length) {
					var cDisplayObject:DisplayObject = _aDisplayList[0] as DisplayObject;
					_aDisplayList.splice(0, 1);
					if(cDisplayObject){
						_appStage.addChild(cDisplayObject);
					}
				}
			}
		}
	}

}